import React,{useEffect} from 'react';
import {lazyImg} from "../../assets/js/utils";
import {Form} from 'antd';
import request from '../../assets/js/utils/request';

function hoc(WitchComponent) {
    function HocComponent(props) {
        //多选框
        const [form] = Form.useForm();
        const [checkedList, setCheckedList] = React.useState(props.defaultCheckedList);
        const [indeterminate, setIndeterminate] = React.useState(false);
        const [checkAll, setCheckAll] = React.useState(false);
        const [loading,setLoading]= React.useState(false);
        const [isSuccess,setSuccess]= React.useState(false)
        const onChange = list => {
            setCheckedList(list);
            setIndeterminate(!!list.length && list.length < props.plainOptions.length);
            setCheckAll(list.length === props.plainOptions.length);
        };
        const onCheckAllChange = e => {
            setCheckedList(e.target.checked ? props.plainOptions : []);
            setIndeterminate(false);
            setCheckAll(e.target.checked);
            form.setFieldsValue({
                'checkbox': e.target.checked ? props.plainOptions : []
            });
        };
        // 提交成功
        const onFinish = (values)=>{
            setLoading(true);
            let sUrl=window.base.config.baseUrl+'api/message',
                data={
                    company_name:values.company_name,
                    name:values.Xname,
                    mobile:values.phone,
                    desc:values.description,
                    link:window.location.href,
                    url:window.location.host,
                    jobs:values.position,
                    product:values.checkbox,
                    email:values.email,
                    // address:address,
                    scale:values.company_size,
                    // source:messageSource // 来源BU
                };
            request(sUrl,'post',data).then((res)=>{
                if (res.code===0){
                    setLoading(false)
                    setSuccess(true)
                    setTimeout(()=>{
                        setSuccess(false)
                        //清空表单数据
                        form.resetFields();
                        form.setFieldsValue({
                            'checkbox': '',
                        })
                        setCheckAll(false)
                        setIndeterminate(false)
                    },1000)
                }
            })
            // setTimeout(()=>{
            //     console.log('Success:', values);
            //     setLoading(false)
            //     setSuccess(true)
            //     setTimeout(()=>{
            //         setSuccess(false)
            //     },5000)
            // },5000)
        }
        // 提交失败
        const onFinishFailed = (errorInfo)=>{
            console.log('Failed:', errorInfo);
        }
        //懒加载
        const echo=()=>{
            lazyImg()
        }
        //点击跳转页面
        const goPages=(url)=>{
            let strUrl = "^(://)" +
                "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" +//ftp的user@+
                "(([0-9]{1,3}.){3}[0-9]{1,3}" +// IP形式的URL- 199.194.52.184 +
                "|" +// 允许IP和DOMAIN（域名） +
                "([0-9a-z_!~*'()-]+.)*" +// 域名- www. +
                "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." +// 二级域名 +
                "[a-z]{0,6})" +// first level domain- .com or .museum +
                "(:[0-9]{1,4})?" +// 端口- :80 +
                "((/?)|"  +// a slash isn't required if there is no file name +
                "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
            let reg = new RegExp(strUrl);
            if (!reg.test(url)){
                window.open(url)
            }else {
                props.history.push(window.base.config.path+url)
            }
        }
        useEffect(()=>{
            echo();
            return ()=>{
                //页面离开时执行
            }
        })
        let newProps={
            goPages:goPages,
            onFinish:onFinish,
            onFinishFailed:onFinishFailed,
            onChange:onChange,
            onCheckAllChange:onCheckAllChange,
            checkedList:checkedList,
            indeterminate:indeterminate,
            checkAll:checkAll,
            form:form,
            loading:loading,
            isSuccess:isSuccess
        }
        return (
            <WitchComponent {...newProps} {...props} />
        )
    }
    return HocComponent
}
export default hoc